﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="LastMessageReplayMessengerTests.cs" company="XamlNinja">
//   2011 Richard Griffin and Ollie Riches
// </copyright>
// <summary>
//   Last message replay messenger tests - we follow BDD principles, http://dannorth.net/introducing-bdd/
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace WP7Contrib.Messaging.Tests
{
    using System;
    using Microsoft.Silverlight.Testing;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Messages;
    using ViewModels;

    [TestClass]
    public class LastMessageReplayMessengerTests : SilverlightTest
    {
        [TestMethod]
        public void ShouldSendLastMessage()
        {
            // Given we have a last message replay messenger...
            var messenger = new LastMessageReplayMessenger();

            // Given we have a message to send...
            var message = new UserMessage("Ollie", "Riches", Guid.NewGuid());

            // Given we have a view model for sending a message...
            var viewModel1 = new SenderViewModel(messenger);

            // Given we can send a message...
            viewModel1.SendUserMessage(message);

            // Given we have a view model for receiving a message...
            var viewModel2 = new ReceiverViewModel(messenger);

            // When we register for user message...
            UserMessage receivedMessage = null;
            viewModel2.RegisterForUserMessage(msg => receivedMessage = msg);

            // Then we expect to have received the last message...
            Assert.IsNotNull(receivedMessage);
            Assert.IsTrue(receivedMessage.Identifier == message.Identifier);
            Assert.IsTrue(receivedMessage.FirstName == message.FirstName);
            Assert.IsTrue(receivedMessage.LastName == message.LastName);
        }

        [TestMethod]
        public void ShouldOnlySendLastMessage()
        {
            // Given we have a last message replay messenger...
            var messenger = new LastMessageReplayMessenger();

            // Given we have a couple of messages to send...
            var message1 = new UserMessage("Ollie", "Riches", Guid.NewGuid());
            var message2 = new UserMessage("Richard", "Griffin", Guid.NewGuid());

            // Given we have a view model for sending messages...
            var viewModel1 = new SenderViewModel(messenger);

            // Given we can send the messages...
            viewModel1.SendUserMessage(message1);
            viewModel1.SendUserMessage(message2);

            // Given we have a view model for receiving a message...
            var viewModel2 = new ReceiverViewModel(messenger);

            // When we register for user message...
            var count = 0;
            UserMessage receivedMessage = null;
            viewModel2.RegisterForUserMessage(msg =>
                                                  {
                                                      count++;
                                                      receivedMessage = msg;
                                                  });

            // Then we expect to have received the last message...
            Assert.IsTrue(count == 1);
            Assert.IsNotNull(receivedMessage);
            Assert.IsTrue(receivedMessage.Identifier == message2.Identifier);
            Assert.IsTrue(receivedMessage.FirstName == message2.FirstName);
            Assert.IsTrue(receivedMessage.LastName == message2.LastName);
        }

        [TestMethod]
        public void ShouldNotSendLastMessage()
        {
            // Given we have a last message replay messenger...
            var messenger = new LastMessageReplayMessenger();

            // Given we have a message to send which we don't want replaying...
            var message = new UserMessageWithIgnoreReplay("Ollie", "Riches", Guid.NewGuid());

            // Given we have a view model for sending a message...
            var viewModel1 = new SenderViewModel(messenger);

            // Given we can send a message...
            viewModel1.SendUserMessage(message);

            // Given we have a view model for receiving a message...
            var viewModel2 = new ReceiverViewModel(messenger);

            // When we register for user message 2...
            UserMessageWithIgnoreReplay receivedMessage = null;
            viewModel2.RegisterForUserMessage(msg => receivedMessage = msg);

            // Then we do not expect to have received a message...
            Assert.IsNull(receivedMessage);
        }
    }
}